package ar.com.e2solver.solver.operator.impl;

import ar.com.e2solver.model.Puzzle;
import ar.com.e2solver.solver.Individual;
import ar.com.e2solver.solver.RandomConstants;
import ar.com.e2solver.solver.operator.MutationOperator;

public final class RotateMutationOperator implements MutationOperator {
	
	private static RotateMutationOperator instance = new RotateMutationOperator();
	
	private RotateMutationOperator(){}

	public static RotateMutationOperator getInstance() {
		return instance;
	}

	public Individual operate(Individual individual) {
		Puzzle puzzle = new Puzzle(individual.getPuzzle());
		
		// La rotacion es solo con las piezas internas (ni bordes ni esquinas)
		if(puzzle.getRowsNumber() > 2 && puzzle.getColumnNumbers() > 2){
			int xRandom = RandomConstants.RANDOM.nextInt(puzzle.getRowsNumber() - 2) + 1;
			int yRandom = RandomConstants.RANDOM.nextInt(puzzle.getColumnNumbers() - 2) + 1;
			puzzle.getPieceAt(xRandom, yRandom).rotateToRight(1);
		}
		
		return new Individual(puzzle);
	}

}
